package com.maimang.platform.crm.util;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.maimang.platform.crm.config.Constant;
import com.maimang.platform.crm.config.URLConstant;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;


/**
 * 获取access_token工具类
 */
public class AccessTokenUtil {
	private static final Logger bizLogger = LoggerFactory.getLogger(AccessTokenUtil.class);

	public static String getToken() throws RuntimeException {
		try {
			DefaultDingTalkClient client = new DefaultDingTalkClient(URLConstant.GET_TOKKEN);
			OapiGettokenRequest request = new OapiGettokenRequest();

			request.setAppkey(Constant.APP_KEY);
			request.setAppsecret(Constant.APP_SECRET);
			request.setHttpMethod("GET");
			OapiGettokenResponse response = client.execute(request);
			String accessToken = response.getAccessToken();
			return accessToken;
		} catch (ApiException e) {
			bizLogger.error("getAccessToken failed", e);
			throw new RuntimeException();
		}
	}

	public static void main(String[] args) throws ApiException {
		String accessToken = AccessTokenUtil.getToken();
		System.out.println("Access-Token is " + accessToken);

		String[] arr = new String[]{"1" , "2", "3", "4", "5"};
		System.out.println(Arrays.toString(arr));
		System.out.println(Arrays.asList(arr));
	}
}
